import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Vant from 'vant'
import 'vant/lib/index.css'
import '@/assets/css/reset.css'
import '@/assets/css/public.css'
import { loginByWXOC } from '@/api/index'
import { getToken, setToken } from '@/service/cookie'
import VueAMap, { initAMapApiLoader } from '@vuemap/vue-amap'
import '@vuemap/vue-amap/dist/style.css'
initAMapApiLoader({
  key: '991c03c4524f9fb552a2fa8442ccb49a'
})
// import VConsole from 'vconsole'
// window.vConsole = new VConsole()
// 测试
// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZWFsZXN0YXRlLm9yYW1hZ2UuY29tIiwiYXVkIjoicmVhbGVzdGF0ZS5vcmFtYWdlLmNvbSIsImlhdCI6MTY1MTE5ODU3MywibmJmIjoxNjUxMTk4NTczLCJleHAiOjQ4MDQ3OTg1NzMsImp0aSI6W3sidXNlcl9pZCI6MzgyMCwiY29tcGFueV9pZCI6MTY0MiwiYmluZF9pZCI6NDB9LDFdfQ.oANvoo41dNsyjf2pgVf9VP9mcp2QGT7JdJoGQt5fkrg
// 正式
// eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJoNS5zcmVhLmNvbS5jbiIsImF1ZCI6Img1LnNyZWEuY29tLmNuIiwiaWF0IjoxNjUwOTU1MzY2LCJuYmYiOjE2NTA5NTUzNjYsImV4cCI6NDgwNDU1NTM2NiwianRpIjpbeyJ1c2VyX2lkIjoxOTgsImNvbXBhbnlfaWQiOjE4MjgsImJpbmRfaWQiOjI2OX0sMV19.dc7Px8dmx-Yq2lMfykch6Xv3yjjGOtvkRjJ8nUXLArk
// 本地调试要放开
// setToken('token', 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZWFsZXN0YXRlLm9yYW1hZ2UuY29tIiwiYXVkIjoicmVhbGVzdGF0ZS5vcmFtYWdlLmNvbSIsImlhdCI6MTcxNjQzNzk1NSwibmJmIjoxNzE2NDM3OTU1LCJleHAiOjQ4NzAwMzc5NTUsImp0aSI6W3sidXNlcl9pZCI6NDIzLCJjb21wYW55X2lkIjo0MjEyLCJiaW5kX2lkIjo2NX0sMV19.4UpbNp1xrqf0U6u9hjd7pMPv3lca65IywIHCf6u0UNc')
// 本地调试要放开
// setToken('bindId', '')
// 本地调试要放开
// setToken('userId', 423)

const bindId = getToken('bindId')
const token = getToken('token')

if (!token && (bindId === undefined || bindId === 'undefined' || !bindId)) {
  if (window.location.href.indexOf('code') === 0 || window.location.href.indexOf('code') <= 0) {
    window.location.replace(`${import.meta.env.VITE_API_BASEURL}v1/wechat?web_url=${encodeURIComponent(window.location.href)}`)
  } else {
    function getQueryString (name: any) {
      const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      const r = window.location.search.substr(1).match(reg)
      if (r != null) {
        return encodeURIComponent(r[2])
      } else {
        return null
      }
    }
    const code: any = getQueryString('code')
    loginByWXOC({ code: code }).then((res: any) => {
      setToken('bindId', res.bind_id)
      setToken('token', res.token)
      if (res.token) {
        if (res.token && (res.bind_id === undefined || res.bind_id === 'undefined' || !res.bind_id)) {
          store.dispatch('fetchUserInfo')
        }
        window.location.search = ''
        createApp(App).use(Vant).use(router).use(store).use(VueAMap).mount('#app')
      }
    })
  }
}

if (token) {
  createApp(App).use(Vant).use(router).use(store).use(VueAMap).mount('#app')
  const app = createApp(App)
  app.config.compilerOptions.isCustomElement = (tag) => {
    return tag.startsWith('wx-open-launch-weapp')
  }
}
// else {
//   createApp(App).use(Vant).use(router).use(store).mount('#app')
// }
